
var Md5 = require("./md5.js");
// import siteinfo from "./siteinfo.js";
import siteinfo from "./siteinfo.js";


var innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = false;
innerAudioContext.obeyMuteSwitch = false;
innerAudioContext.src = 'https://msapp.oss-cn-beijing.aliyuncs.com/mp-games/haichong/mp3/gunfire1.m4a';
innerAudioContext.onCanplay(() => {
	console.log('枪声1：','枪声进入可以播放状态');
});
innerAudioContext.onError((res) => {
	if(res.errCode==10001){
		//uni.showToast({icon: 'none',title:'系统错误',duration: 2000});
	}else if(res.errCode==10002){
		//uni.showToast({icon: 'none',title:'网络错误',duration: 2000});
	}else if(res.errCode==10003){
		//uni.showToast({icon: 'none',title:'文件错误',duration: 2000});					
	}else if(res.errCode==10004){
		//uni.showToast({icon: 'none',title:'格式错误',duration: 2000});
	}else{
		//uni.showToast({icon: 'none',title:'未知错误',duration: 2000});					
	}
	console.log('枪声1：','枪声播放错误');
	console.log('枪声1：',res.errMsg);
	console.log('枪声1：',res.errCode);
});




module.exports = {
	
	//微信分享
	onShareApp : function (leix='hy'){
		var shareTitle = siteinfo.shareTitle;
		var shareImage = siteinfo.shareImage;
		var sharePath = siteinfo.shareUrl;
		if(leix=='pyq'){
			var shareData = {
			    title: shareTitle,
			    query: sharePath
			};
			if(shareImage){shareData.imageUrl = shareImage;}
		}else{
			var shareData = {
				title: shareTitle,
				path: sharePath
			};
			if(shareImage){shareData.imageUrl = shareImage;}			
		}
		return shareData;
	},	
	
	
	//枪声
	shootPlay : function(){
		innerAudioContext.seek(0);
		innerAudioContext.startTime = 0;
		innerAudioContext.play();
	},
	
	//请求
	post: function(api, data, callback) {
		
		
		callback({
			errno: 1,
			data: {
				'userLevel':5,
				'jifen':11,
				'gongxian':22,
				'uni_emit':{
					//uni.$emit 触发全局通讯方法名（uni_emit）,用户上一个页面数据更新，如积分变动
				}
			},
			message: ''
		});
		
		console.log('后端请求方法开发者自行封装');
		return;
		
		
		//示列
		wx.login({
			success (res) {
				
				var Time = new Date().getTime();
				var postData = data;
				    postData['apitype'] = api;
					postData['code'] = res.code;
					postData['token'] = siteinfo.token;
					postData['mpapp'] = 'wxapp';
				
				var jsData = JSON.stringify(postData);
				var Key = Md5.md5(jsData + Time + 'wgaString');
				
				var posData = {
					app_data: jsData,
					app_time: Time,
					app_key: Key
				};
					
				uni.request({
					url: siteinfo.apiUrl,
					data: posData,
					method: "POST",
					dataType: "json",
					header: {'content-type': 'application/x-www-form-urlencoded'},
					success: (res) => {console.log('res',res.data);
						if (res.statusCode === 200) {
							callback(res.data);
						} else {
							callback({
								errno: res.statusCode,
								message: res.errMsg
							});
						}
					},
					fail: () => {
						callback({
							errno: 3,
							message: '网络请求超时,请重试'
						});
					}
				});
			},
			fail(e) {
				callback({
					errno: 3,
					message: '获取微信登录凭证失败'
				});
			}
		});
	},
	
	
	
	//随机数(0,100)
	randomNum : function(minNum,maxNum){
	    switch(arguments.length){
	        case 1: 
	            return parseInt(Math.random()*minNum+1,10); 
	        break; 
	        case 2: 
	            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
	        break; 
	            default: 
	                return 0; 
	            break; 
	    }  
	}
}
